local zhiheng = fk.CreateSkill {
  name = "lingling__zhiheng",
}

Fk:loadTranslationTable {
  ["lingling__zhiheng"] = "制衡",
  [":lingling__zhiheng"] = "每回合限两次，当你不以此法获得牌后，你可以弃置两张颜色不同的牌，然后摸两张牌并获得一张【影】。若你已经发动“释权”，每回合第二次发动此技能时改为摸三张牌。",

  ["#lingling__zhiheng-invoke"] = "制衡：你可以弃置两张颜色不同的牌，摸两张牌并获得一张【影】",
  ["#lingling__zhiheng-invoke2"] = "制衡：你可以弃置两张颜色不同的牌，摸三张牌",
}

local U = require "packages/lingling/util"

zhiheng:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zhiheng.name) and #player:getCardIds("he") > 1 and
        player:usedSkillTimes(zhiheng.name, Player.HistoryTurn) < 2 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand and move.skillName ~= zhiheng.name then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#lingling__zhiheng-invoke"
    if player:usedSkillTimes("lingling__shiquan",Player.HistoryGame) > 0 and player:usedSkillTimes(zhiheng.name,Player.HistoryTurn) > 0 then
      prompt = "#lingling__zhiheng-invoke2"
    end
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "lingling__zhiheng_active",
      prompt = prompt,
      cancelable = true,
    })
    if success and dat then
      event:setCostData(self, { cards = dat.cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, zhiheng.name, player, player)
    if not player.dead then
      if player:usedSkillTimes("lingling__shiquan",Player.HistoryGame) > 0 and player:usedSkillTimes(zhiheng.name,Player.HistoryTurn) > 0 then
        player:drawCards(3,zhiheng.name)
      else
        player:drawCards(2, zhiheng.name)
        if player.dead then return end
        local card = U.getShade(room, 1)
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, zhiheng.name, nil, true, player)
      end
    end
  end,
})

return zhiheng
